package com.cxrj.pqm.pqmManagement.service.impl;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.NumberFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.cxrj.pqm.pqmManagement.dao.PqmItemMapper;
import com.cxrj.pqm.pqmManagement.entity.PqmItemWithBLOBs;
import com.cxrj.pqm.pqmManagement.service.PqmService;
import com.cxrj.pqm.utils.PageBean;

@Service
public class PqmServiceImpl implements PqmService {

	@Resource
	private PqmItemMapper pqmItemMapper;

	@Override
	public Map<String, Object> insertAndUpdateItem(PqmItemWithBLOBs pqmItemWithBLOBs, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		Integer a;
		if (pqmItemWithBLOBs.getItemId() == null || pqmItemWithBLOBs.getItemId().equals("")) {
			pqmItemWithBLOBs.setItemId(UUID.randomUUID().toString());
			pqmItemWithBLOBs.setUpdatetime(new Date());
			pqmItemWithBLOBs.setS1(getComplete(pqmItemWithBLOBs));
			pqmItemWithBLOBs.setUserId(request.getSession().getAttribute("openid") + "");
			a = pqmItemMapper.insertSelective(pqmItemWithBLOBs);
		} else {
			pqmItemWithBLOBs.setUpdatetime(new Date());
			pqmItemWithBLOBs.setS1(getComplete(pqmItemWithBLOBs));
			a = pqmItemMapper.updateByPrimaryKeySelective(pqmItemWithBLOBs);
		}
		map.put("result", 0);
		if (a != 0) {
			map.put("result", 1);
		}
		return map;
	}

	@Override
	public Map<String, Object> selectItem(String itemName, String index, String size, String type, String startTime, String endTime, String sorting) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("itemName", itemName);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("sorting", sorting);
		map.put("type", type);
		map.put("index", Integer.parseInt(index));
		map.put("size", Integer.parseInt(size));

		PageBean bean = new PageBean();
		bean.setPageIndex(Integer.parseInt(index));
		bean.setPageSize(Integer.parseInt(size));
		bean.setPageCount(pqmItemMapper.selectByFY(map).size());

		resultMap.put("pageBean", bean);
		List<PqmItemWithBLOBs> list = pqmItemMapper.selectByFY(map);
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectByItemID(String itemID) {
		Map<String, Object> map = new HashMap<>();
		map.put("item", pqmItemMapper.selectByPrimaryKey(itemID));
		return map;
	}

	@Override
	public Map<String, Object> selectByUserID(String userID, String index, String size, String itemName, String startTime, String endTime, String sorting) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userID", userID);
		map.put("index", Integer.parseInt(index));
		map.put("size", Integer.parseInt(size));
		map.put("itemName", itemName);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("sorting", sorting);

		PageBean bean = new PageBean();
		bean.setPageIndex(Integer.parseInt(index));
		bean.setPageSize(Integer.parseInt(size));
		bean.setPageCount(pqmItemMapper.selectByUserID(map).size());

		resultMap.put("pageBean", bean);
		resultMap.put("list", pqmItemMapper.selectByUserID(map));
		return resultMap;
	}

	/**
	 * 获取完成度
	 * 
	 * @return
	 */
	public static String getComplete(PqmItemWithBLOBs pqmItemWithBLOBs) {
		String complete = "";
		try {
			Class<?> clazz1 = Class.forName("com.cxrj.pqm.pqmManagement.entity.PqmItem");
			Class<?> clazz2 = Class.forName("com.cxrj.pqm.pqmManagement.entity.PqmItemWithBLOBs");
			Field[] fields1 = clazz1.getDeclaredFields();
			Field[] fields2 = clazz2.getDeclaredFields();
			Integer count1 = 0;
			Integer count2 = 0;
			Boolean flag = false;

			for (Field field : fields1) {
				if (field.getName().equals("itemId") || field.getName().equals("updatetime") || field.getName().equals("userId") || field.getName().equals("s1") || field.getName().equals("s2")
						|| field.getName().equals("starttime") || field.getName().equals("endtime") || field.getName().equals("complete") || field.getName().equals("itemType")) {
					continue;
				}
				PropertyDescriptor pd = new PropertyDescriptor(field.getName(), pqmItemWithBLOBs.getClass());
				Method m = pd.getReadMethod();
				if (m.invoke(pqmItemWithBLOBs) != null && (m.invoke(pqmItemWithBLOBs) instanceof String && !m.invoke(pqmItemWithBLOBs).equals(""))) {
					count2++;
				}
				count1++;
			}
			for (Field field : fields2) {
				if (pqmItemWithBLOBs.getItemType().equals("0")) {
					if (field.getName().equals("itemPlan") || field.getName().equals("itemReport") || field.getName().equals("planImg") || field.getName().equals("reportImg")
							|| field.getName().equals("user2Img") || field.getName().equals("user3Img") || field.getName().equals("itemAndImg")) {
						continue;
					}
				}
				if (pqmItemWithBLOBs.getItemType().equals("1")) {
					if (flag || field.getName().equals("itemFile") || field.getName().equals("planImg") || field.getName().equals("reportImg") || field.getName().equals("user2Img")
							|| field.getName().equals("user3Img") || field.getName().equals("itemAndImg")) {
						continue;
					}
				}
				PropertyDescriptor pd = new PropertyDescriptor(field.getName(), pqmItemWithBLOBs.getClass());
				Method m = pd.getReadMethod();
				if (m.invoke(pqmItemWithBLOBs) != null && (m.invoke(pqmItemWithBLOBs) instanceof String && !m.invoke(pqmItemWithBLOBs).equals(""))) {
					count2++;
				}
				count1++;
			}
			NumberFormat numberFormat = NumberFormat.getInstance();
			numberFormat.setMaximumFractionDigits(2);
			complete = numberFormat.format((float) count2 / (float) count1 * 100) + "%";
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return complete;
	}

}
